<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/4-11:24
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\model;


    use app\admin\imple\Comm;
    use app\common\model\BaseModel;
    use think\db\Where;
    use think\Exception;
    use think\facade\Url;

    class Models extends BaseModel implements Comm
    {


        protected $name ="models";

        protected function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

        }



        /**
         * @return array
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: adminGetModelSelect
         * @describe:/获取所有的模型数组
         */
        public function adminGetModelSelect(){
            $data=$this->order('sort desc')->select()->toArray();
            return $data;
        }



        /**
         * @param $id
         * @return array|bool|\PDOStatement|string|\think\Model|null
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: adminGetTableNameToModelId
         * @describe:根据模型id获取数据表名称
         */
        public function adminGetTableNameToModelId($id){
            $data=$this->where(['id'=>$id])->field('id,name,tablename')->find();
            if(empty($data)){
                return false;
            }else{
                $data=$data->toArray();
            }
            return $data;
        }



        /**
         * @param $tablename
         * @return array|bool
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: adminGetModelDataToTableName
         * @describe:根据数据表名称获取model数据
         */
        public function adminGetModelDataToTableName($tablename){
            $data=$this->where(['tablename'=>$tablename])->field('id,name,tablename')->find();
            if(empty($data)){
                return false;
            }
            return $data->toArray();
        }



        /**
         * @param array $where
         * @return array|bool
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/9
         * @name: getOne
         * @describe: 查询单个数据
         */
        public function getOne($where = [])
        {
            if (empty($where)){
                return false;
            }
            return self::where($where)->find();
        }



        /**
         * @return array
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: adminGetModelArray
         * @describe:获取到所有的模型数组   一维结构
         */
        /*
         *  [
         *      'id'=>'tablename',
         *      'id'=>'tablename',
         * ]
         *
         */
        public function adminGetModelArray(){
            $arr=[];
            $data=$this->adminGetModelSelect();
            foreach ($data as $val){
                $arr[$val['id']]=$val['tablename'];
            }
            return $arr;
        }
        /**
         * @param int $id
         * @param string $func
         * @return array|bool|\PDOStatement|string|\think\Model|null
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: adminGetModelSelectAndUrl
         * @describe:获取带不同url的model跳转链接(后台url)
         */
        /*
         * $id 根据id获取  如果为0则获取全部 不为0则获取一个
         * $func  控制器下的方法  如：admin/Article/index  or admin/Article/edit
         */

        public function adminGetModelSelectAndUrl($id=0,$func='index'){
            if($id==0){
                $arr=[];
                $all_data=$this->adminGetModelSelect();
                foreach ($all_data as $val){
                    $val['url']=url("".$val['tablename']."/".$func."");
                    $arr[]=$val;
                }
                return $arr;
            }else{
                $one_data=$this->adminGetTableNameToModelId($id);
                $one_data['url']=url("".$one_data['tablename'] ."/".$func."");
                return $one_data;
            }
        }



        /**
         * @param $model_id
         * @param array $param
         * @param string $func
         * @return string
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: getUrlToModelId
         * @describe:生成url跳转链接
         */
        /*
         * $model_id 模型id
         * $param ['param1'=>'id or category','param2'=>'$id']
         * $func  控制器下的方法  如：index/Article/index  or index/Article/edit
         */
        public function getUrlToModelId($model_id,$param=[],$func='index'){
            $model_data=$this->adminGetTableNameToModelId($model_id);
            Url::root('/');
            $url=url('index/'.$model_data['tablename'].'/'.$func,[$param['param1']=>$param['param2']]);
            return $url;
        }



        /**
         * @param array $param
         * @param string $order
         * @return mixed|void
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: getAdminPageData
         * @describe:
         */
        public function getAdminPageData($param = [], $order = 'id asc')
        {
            // TODO: Implement getAdminPageData() method.
            $where = [];
            if(!empty($param)) {
                //搜索条件
                if(!empty($param['name'])) {
                    $where['name'] = ['like', "%".$param['name']."%"];
                    //$where[] = ['title','like', "%".$param['title']."%"];
                }
            }
            try{
                $data = self::where(new Where($where))->order($order)->select()->toArray();
                $count =self::where(new Where($where))->count("id");

                $this->ajaxResult($data,0,'',$count);

            }catch (Exception $exception){

                $this->ajaxResult('',100,$exception->getMessage());
            }
        }



        /**
         * @param $data
         * @return mixed|void
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: addAndEdit
         * @describe:
         */
        public function addAndEdit($data)
        {
            // TODO: Implement addAndEdit() method.
            return parent::doAll($data);
        }



        /**
         * @param $id
         * @return bool|mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: del
         * @describe:
         */
        public function del($id)
        {
            // TODO: Implement del() method.
            return parent::JrkDelete($id);
        }
    }